/**
 * @author p s j
 */

    /*
     * 쪽지 View
     */
    function letterWrite_View(){
        this.HTMLDom = null;
        this.sendMessageData = null;
        this._init();
    }
    letterWrite_View.prototype = {
        _init : function(){
            this.HTMLDom = {
                "$idLayer" : $("#message_target")
              , "$sendText" : $("#send_message")
            }
            this.display();
        }
        , getUserId : function(){
              return document.globalObj.PROFILE_DATA.userId;
        }
        , getUserNickName : function(){
              return document.globalObj.PROFILE_DATA.nickName;
        }
        , getMessageTxt : function(){
            return this.HTMLDom["$sendText"].val();
        }
        , display  : function(){
           try{
               var $IDlayer = this.HTMLDom["$idLayer"];
                var targetNick = this.getUserNickName();
               if(targetNick){
                   $IDlayer.text(targetNick);
               }
           }catch(e){
               console.log("targetNick name not find error >>>>" + e);
           }
            
        }
        , spinStart: function(){      
            $.mobile.showPageLoadingMsg();          
        }
        , spinEnd: function(){          
            $.mobile.hidePageLoadingMsg();              
        }   
        
    }
    /*
     쪽지 보내기 연동
     */
    function letterWrite_Comm(){
        //this._init();
        this.messageData = null;
    }
    letterWrite_Comm.prototype = {
        _init : function(){
            
        }
       , sendMessage : function(){
           var _this = this;
           var objJson = new MJson();
            var cm = new ConnectionManager();
            objJson.putPub(MJson.PUB.SID, "MG001");
            objJson.putPub(MJson.PUB.SERVICENAME, "sendMessage");
            objJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
            objJson.putParameter("recUserId" , _this.messageData['받는사람'] );
            objJson.putParameter("messageBody",  _this.messageData['쪽지내용'] );
    
            cm.requestGet(objJson.getJsonString(), function(data){
               
                console.log(data);
                if(data.msp.contents.serviceResult == "00000"){
                    var dataObj = data.msp.contents.items; 
                    console.log("success!!");
                    $(_this).trigger("sendFinish");
                }
            }, function($err) {
                console.log("errod >>>>>>>>>");
                console.log($err);
                alert("쪽지 전송 실패")
            });
       }
       , setMessageData : function(data){
           this.messageData = {
               "받는사람" : data["받는사람"]
               ,"쪽지내용" : encodeURIComponent(data['쪽지내용'] )
           }
           
           console.log(this.messageData);
           if(this.messageData["쪽지내용"].length  < 1)  {
               alert("쪽지내용을 입력해 주십시오");
               $("textarea").trigger("focus");
               return; 
           }
           this.sendMessage();
       }
       , getMesssageData : function(){
           
       }
    }
    /*
     * 쪽지 보내기  
     */
    function Letter_Write(){
       this.message_data = null;
       this.letterWriteView = null;
       this.letterWriteComm = null;
    }
    
    Letter_Write.prototype = {
        onPageShow : function(){
            $("a").attr("href","");
            this._init();
        }
         , _init : function(){
             this.letterWriteView = new letterWrite_View();
             this.letterWriteComm = new letterWrite_Comm();
             this._bind();
         }
          , _bind : function(){
              //console.log('==bind==')
              var _this = this;
              console.log($("#send_btn"))
              $("#send_btn").click( function(){
                  _this.letterWriteView.spinStart();
                    _this.message_data = {
                        "받는사람" : _this.letterWriteView.getUserId()
                     ,   "쪽지내용" : _this.letterWriteView.getMessageTxt()
                    }
                    _this.letterWriteComm.setMessageData(_this.message_data);
              });
              $("#send_cancel_btn").on("click" , function(){
                  //취소 버튼
              });
              $(_this.letterWriteComm).on('sendFinish' , function(event){
                  console.log("성공 이벤트 call");
                  _this.letterWriteView.spinEnd();
                 var n =  confirm("쪽지가 발송되었습니다. 보낸 쪽지함으로 이동하시겠습니까?");
                 if(n) {
                     _this._clear();
                     $.mobile.changePage("../inc/letter_send.html"); 
                 }
                 else $.mobile.changePage("../mypage/profile_view.html");    
              });
          }
          , _clear : function(){
              //전송 완료 후 global 변수 값 초기화 
              document.globalObj.PROFILE_DATA.userId = null;
              document.globalObj.PROFILE_DATA.nickName = null;
          }
          
       
    }